//
//  OilStationViewController.swift
//  DSHCMerchant
//
//  Created by 罗德良 on 2018/4/26.
//  Copyright © 2018年 袋鼠好车. All rights reserved.
//

import UIKit
import Alamofire

class OilStationViewController: BaseViewController {

    @IBOutlet var oilGunNumTF: UITextField!
    
    @IBOutlet var priceTF: UITextField!
    
    @IBOutlet var sumbitBtn: UIButton!
    
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.view.backgroundColor = UIColor.white
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        baseSetNavRightButtonWithTitle("历史记录")
        
    }
    override func baseNavRightButtonPressed(_ button: UIButton) {
        let oilHistoryVC = OilHistoryViewController()
        oilHistoryVC.title = "历史记录"; self.navigationController?.pushViewController(oilHistoryVC, animated: true)
    }
    
    
    @IBAction func sumbitBtnPress(_ sender: UIButton) {
        if (self.oilGunNumTF.text == "") || (self.priceTF.text == ""){
            MBProgressHUD.alertShow(in: self.view, message: "请填写完整再提交", hidenAfter: 0.8, mode: MBProgressHUDMode.text)
            return
        }
        requestAddOil(oilGunNum: oilGunNumTF.text!, price: priceTF.text!)
    }
    
    
    //网络请求
    func requestAddOil(oilGunNum:String,price:String) -> Void {
        
        let param =
            ["product":oilGunNum,
             "price":price,
             "sid":UserModel.shareUser.data?.storeId ?? String(),
                "uid":UserModel.shareUser.data?.uid ?? String()
                
        ]
        
        Alamofire.request(addOilUrl, method:.post, parameters:param)
            .responseString { response in
                
                switch response.result.isSuccess {
                case true:
                    
                    var basemodel = DefaultData()
                    basemodel = DefaultData.deserialize(from: response.value)!
                    
                    switch basemodel.status! {
                    case true:
                        print("数据请求成功")
                        let oilHistoryVC = OilHistoryViewController()
                        oilHistoryVC.title = "历史记录"; self.navigationController?.pushViewController(oilHistoryVC, animated: true)
                    case false:
                         MBProgressHUD.alertShow(in: self.view, message:basemodel.msg, hidenAfter: 0.8, mode: MBProgressHUDMode.text)
                    }
                    
                case false:
                    print(response.result.error!)
                }
                
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
